<template>
  <div class="app-container">
    <FilterForm :data="filterData" @search="getList" labelWidth="86px" />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:goods:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:goods:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:goods:remove']"
          >删除</el-button
        >
      </el-col>
    </el-row>

    <Table
      :api="$Api.business.name.list"
      :selection="true"
      @select="handleSelectionChange"
      ref="Table"
      height="60vh"
    >
      <el-table-column label="品名" align="center" prop="goodsName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.native.stop="handleUpdate(scope.row)"
            v-hasPermi="['business:goods:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click.native.stop="handleDelete(scope.row)"
            v-hasPermi="['business:goods:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </Table>

    <!-- 添加或修改木业商品信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品名" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入品名" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="item in isStatus"
              :key="item.dictValue"
              :label="item.dictValue"
              >{{ item.dictLabel}}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsName: [{ required: true, message: "请输入品名", trigger: "blur" }],
      },
      isStatus: [],
      filterData: [
        {
          label: "品名",
          name: "goodsName",
          type: "none",
        },
        {
          label: "状态",
          name: "status",
          type: "select",
          select: {
            option: [],
            selectLabel: "dictLabel",
            selectValue: "dictValue",
            multiple: true,
          },
        },
      ],
    };
  },
  created() {
      this.getDicts("sys_normal_disable").then(res => {
        let { data } = res;
        this.isStatus = data;
        this.filterData.forEach( (v) =>{
          if( v.name == "status") {
              v.select.option = data;
          }
        })
      });
  },
  methods: {
    /** 查询木业商品信息列表 */
    getList(queryParams) {
      this.$refs.Table.getData(queryParams);
    },
// 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.isStatus, row.status);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.basicsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsName: null,
        status: "0",
        remark: null,
      };
      this.resetForm("form");
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加品名信息";
    },
    // 修改商品管理状态
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.goodsName + '"吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then( ()=>{
            this.$Api.business.name.update(row)
            .then( (res)=>{
                  let { code } = res;
                  if( code == 200 ) {
                      this.getList();
                      this.msgSuccess(text + "成功");
                  }
              })
        }).catch( ()=>{
           row.status = row.status === "0" ? "1" : "0";
        })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsId = row.goodsId || this.ids;
        this.$Api.business.name.detail(goodsId).then((res) => {
        this.form = res.data;
        this.open = true;
        this.title = "修改品名信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      let { goodsId } = this.form;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (goodsId != null) {
            this.$Api.business.name.update(this.form).then((res) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.$Api.business.name.add(this.form).then((res) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids;
      this.$confirm("是否确认删除品名的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then( ()=>{
                this.$Api.business.name.del(goodsIds)
                .then( (res)=>{
                    let { code } = res;
                    if( code == 200 ) {
                        this.getList();
                        this.msgSuccess("删除成功");
                    }
                })
      }).catch( ()=>{})    
    },
  },
};
</script>
